﻿using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.IO;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.IO;

using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.System;

using GPSController;
using Ascended.System;

namespace GPS_Module
{
    public class GPS
    {
        enum GLL_Command { TYPE, LAT, NS, LONG, EW, TIME, STATUS, MODE, CHECKSUM, END };
        static readonly string GLL_HEADER = "$GPGLL";

        SerialPort port = new SerialPort("COM4", 57600, Parity.None, 8, StopBits.One);
        LS20031 myGps;
        public float Longitude, Latitude, Time;
        public int hour, min, sec;
        public char NorSow, EasWes;

        public GPS()
        {

          myGps = new LS20031(port);

          myGps.Init_GPS();

        }

        public void pollGPS(int number)
        {
            //Debug.Print("Polling...");
            for (int i = 0; i < number; i++)
            {
                while (!myGps.Poll_GPS(Output_Data)) ;
                Thread.Sleep(750);
            }
        }

        private void Output_Data(byte[] output)
        {
            char[] outs = new char[output.Length];
            char[] newouts = new char[256];


            string[] Commands = new string[8];
            int number = output.Length;

            //if (number == 255)
            //{
            //    Debug.Print("Failed to Poll");
            //    return;
            //}

            outs = Encoding.UTF8.GetChars(output);


            if (outs[0] == ('G') | outs[0] == ('P')) //Checks to see if the '$' was eatten and adds it back to the front
            {
                string check1 = new string(outs);
                check1 = String.Concat('$', check1);
                outs = check1.ToCharArray();
            }


            string NMEA = new string(outs);
            string[] final = NMEA.Split(new char[] { });

            int i = 0;
            foreach (string command in final)
                if (command.Trim() != "")
                {
                    //Debug.Print(command);
                    Commands[i] = command;
                    i++;
                }

            try
            {
                for (int j = 0; j < i; j++)
                {
                    string[] parse = Commands[j].Split(new char[] { ',' });

                    if (parse[(int)GLL_Command.TYPE].CompareTo(GLL_HEADER) == 0)
                    {
                        //Debug.Print("\n-------------------");

                        Latitude = (ConvertString.ToSingle(parse[(int)GLL_Command.LAT]));
                        //Debug.Print("LAT: " + (Latitude).ToString());
                        Latitude /= 100;
                        Latitude = (float)((int)(Latitude) + (Latitude - (int)Latitude) * ((float)10 / 6));
                        // Debug.Print("LAT: "+ (Latitude).ToString()+" degrees");

                        char[] NS = parse[(int)GLL_Command.NS].ToCharArray();

                        if (NS == null)
                            return;
                        if (NS.Length == 0)
                            return;
                        NorSow = NS[0];
                        Debug.Print("Latitude: " + Latitude.ToString() + " " + NorSow.ToString());

                        Longitude = ConvertString.ToSingle(parse[(int)GLL_Command.LONG]);
                        //Debug.Print("LONG: " + Longitude.ToString());
                        Longitude /= 100;
                        Longitude = (float)((int)(Longitude) + (Longitude - (int)Longitude) * ((float)10 / 6));
                        //Debug.Print("LONG: " + (Longitude).ToString() + " degrees");

                        char[] EW = parse[(int)GLL_Command.EW].ToCharArray();
                        EasWes = EW[0];
                        Debug.Print("Longitude: " + Longitude.ToString() + " " + EasWes.ToString());

                        Time = ConvertString.ToSingle(parse[(int)GLL_Command.TIME]);
                        hour = (int)(Time / 10000);
                        min = (int)((Time - (hour * 10000)) / 100);
                        sec = (int)(Time - (hour * 10000) - (min * 100));
                        //Debug.Print("Time: " + Time.ToString());
                        if (min < 10 && sec < 10)
                            Debug.Print("Time: " + hour + ":0" + min + ":0" + sec);
                        else if (min < 10)
                            Debug.Print("Time: " + hour + ":0" + min + ":" + sec);
                        else if (sec < 10)
                            Debug.Print("Time: " + hour + ":" + min + ":0" + sec);
                        else
                            Debug.Print("Time: " + hour + ":" + min + ":" + sec);
                        Debug.Print("-------------------\n");

                    }
                }
            }
            catch
            {
                Thread.Sleep(20000);
                return;
            }
        }

    }
}